{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rational Numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from fractions import Fraction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can get some info on the Fraction class:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on class Fraction in module fractions:\n",
      "\n",
      "class Fraction(numbers.Rational)\n",
      " |  This class implements rational numbers.\n",
      " |  \n",
      " |  In the two-argument form of the constructor, Fraction(8, 6) will\n",
      " |  produce a rational number equivalent to 4/3. Both arguments must\n",
      " |  be Rational. The numerator defaults to 0 and the denominator\n",
      " |  defaults to 1 so that Fraction(3) == 3 and Fraction() == 0.\n",
      " |  \n",
      " |  Fractions can also be constructed from:\n",
      " |  \n",
      " |    - numeric strings similar to those accepted by the\n",
      " |      float constructor (for example, '-2.3' or '1e10')\n",
      " |  \n",
      " |    - strings of the form '123/456'\n",
      " |  \n",
      " |    - float and Decimal instances\n",
      " |  \n",
      " |    - other Rational instances (including integers)\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      Fraction\n",
      " |      numbers.Rational\n",
      " |      numbers.Real\n",
      " |      numbers.Complex\n",
      " |      numbers.Number\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __abs__(a)\n",
      " |      abs(a)\n",
      " |  \n",
      " |  __add__(a, b)\n",
      " |      a + b\n",
      " |  \n",
      " |  __bool__(a)\n",
      " |      a != 0\n",
      " |  \n",
      " |  __ceil__(a)\n",
      " |      Will be math.ceil(a) in 3.0.\n",
      " |  \n",
      " |  __copy__(self)\n",
      " |  \n",
      " |  __deepcopy__(self, memo)\n",
      " |  \n",
      " |  __eq__(a, b)\n",
      " |      a == b\n",
      " |  \n",
      " |  __floor__(a)\n",
      " |      Will be math.floor(a) in 3.0.\n",
      " |  \n",
      " |  __floordiv__(a, b)\n",
      " |      a // b\n",
      " |  \n",
      " |  __ge__(a, b)\n",
      " |      a >= b\n",
      " |  \n",
      " |  __gt__(a, b)\n",
      " |      a > b\n",
      " |  \n",
      " |  __hash__(self)\n",
      " |      hash(self)\n",
      " |  \n",
      " |  __le__(a, b)\n",
      " |      a <= b\n",
      " |  \n",
      " |  __lt__(a, b)\n",
      " |      a < b\n",
      " |  \n",
      " |  __mod__(a, b)\n",
      " |      a % b\n",
      " |  \n",
      " |  __mul__(a, b)\n",
      " |      a * b\n",
      " |  \n",
      " |  __neg__(a)\n",
      " |      -a\n",
      " |  \n",
      " |  __pos__(a)\n",
      " |      +a: Coerces a subclass instance to Fraction\n",
      " |  \n",
      " |  __pow__(a, b)\n",
      " |      a ** b\n",
      " |      \n",
      " |      If b is not an integer, the result will be a float or complex\n",
      " |      since roots are generally irrational. If b is an integer, the\n",
      " |      result will be rational.\n",
      " |  \n",
      " |  __radd__(b, a)\n",
      " |      a + b\n",
      " |  \n",
      " |  __reduce__(self)\n",
      " |      helper for pickle\n",
      " |  \n",
      " |  __repr__(self)\n",
      " |      repr(self)\n",
      " |  \n",
      " |  __rfloordiv__(b, a)\n",
      " |      a // b\n",
      " |  \n",
      " |  __rmod__(b, a)\n",
      " |      a % b\n",
      " |  \n",
      " |  __rmul__(b, a)\n",
      " |      a * b\n",
      " |  \n",
      " |  __round__(self, ndigits=None)\n",
      " |      Will be round(self, ndigits) in 3.0.\n",
      " |      \n",
      " |      Rounds half toward even.\n",
      " |  \n",
      " |  __rpow__(b, a)\n",
      " |      a ** b\n",
      " |  \n",
      " |  __rsub__(b, a)\n",
      " |      a - b\n",
      " |  \n",
      " |  __rtruediv__(b, a)\n",
      " |      a / b\n",
      " |  \n",
      " |  __str__(self)\n",
      " |      str(self)\n",
      " |  \n",
      " |  __sub__(a, b)\n",
      " |      a - b\n",
      " |  \n",
      " |  __truediv__(a, b)\n",
      " |      a / b\n",
      " |  \n",
      " |  __trunc__(a)\n",
      " |      trunc(a)\n",
      " |  \n",
      " |  limit_denominator(self, max_denominator=1000000)\n",
      " |      Closest Fraction to self with denominator at most max_denominator.\n",
      " |      \n",
      " |      >>> Fraction('3.141592653589793').limit_denominator(10)\n",
      " |      Fraction(22, 7)\n",
      " |      >>> Fraction('3.141592653589793').limit_denominator(100)\n",
      " |      Fraction(311, 99)\n",
      " |      >>> Fraction(4321, 8765).limit_denominator(10000)\n",
      " |      Fraction(4321, 8765)\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  from_decimal(dec) from abc.ABCMeta\n",
      " |      Converts a finite Decimal instance to a rational number, exactly.\n",
      " |  \n",
      " |  from_float(f) from abc.ABCMeta\n",
      " |      Converts a finite float to a rational number, exactly.\n",
      " |      \n",
      " |      Beware that Fraction.from_float(0.3) != Fraction(3, 10).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(cls, numerator=0, denominator=None, *, _normalize=True)\n",
      " |      Constructs a Rational.\n",
      " |      \n",
      " |      Takes a string like '3/2' or '1.5', another Rational instance, a\n",
      " |      numerator/denominator pair, or a float.\n",
      " |      \n",
      " |      Examples\n",
      " |      --------\n",
      " |      \n",
      " |      >>> Fraction(10, -8)\n",
      " |      Fraction(-5, 4)\n",
      " |      >>> Fraction(Fraction(1, 7), 5)\n",
      " |      Fraction(1, 35)\n",
      " |      >>> Fraction(Fraction(1, 7), Fraction(2, 3))\n",
      " |      Fraction(3, 14)\n",
      " |      >>> Fraction('314')\n",
      " |      Fraction(314, 1)\n",
      " |      >>> Fraction('-35/4')\n",
      " |      Fraction(-35, 4)\n",
      " |      >>> Fraction('3.1415') # conversion from numeric string\n",
      " |      Fraction(6283, 2000)\n",
      " |      >>> Fraction('-47e-2') # string may include a decimal exponent\n",
      " |      Fraction(-47, 100)\n",
      " |      >>> Fraction(1.47)  # direct construction from float (exact conversion)\n",
      " |      Fraction(6620291452234629, 4503599627370496)\n",
      " |      >>> Fraction(2.25)\n",
      " |      Fraction(9, 4)\n",
      " |      >>> Fraction(Decimal('1.47'))\n",
      " |      Fraction(147, 100)\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  denominator\n",
      " |  \n",
      " |  numerator\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  __abstractmethods__ = frozenset()\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from numbers.Rational:\n",
      " |  \n",
      " |  __float__(self)\n",
      " |      float(self) = self.numerator / self.denominator\n",
      " |      \n",
      " |      It's important that this conversion use the integer's \"true\"\n",
      " |      division rather than casting one side to float before dividing\n",
      " |      so that ratios of huge integers convert without overflowing.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from numbers.Real:\n",
      " |  \n",
      " |  __complex__(self)\n",
      " |      complex(self) == complex(float(self), 0)\n",
      " |  \n",
      " |  __divmod__(self, other)\n",
      " |      divmod(self, other): The pair (self // other, self % other).\n",
      " |      \n",
      " |      Sometimes this can be computed faster than the pair of\n",
      " |      operations.\n",
      " |  \n",
      " |  __rdivmod__(self, other)\n",
      " |      divmod(other, self): The pair (self // other, self % other).\n",
      " |      \n",
      " |      Sometimes this can be computed faster than the pair of\n",
      " |      operations.\n",
      " |  \n",
      " |  conjugate(self)\n",
      " |      Conjugate is a no-op for Reals.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors inherited from numbers.Real:\n",
      " |  \n",
      " |  imag\n",
      " |      Real numbers have no imaginary component.\n",
      " |  \n",
      " |  real\n",
      " |      Real numbers are their real component.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(Fraction)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can create Fraction objects in a variety of ways:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using integers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(1, 1)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(1, 3)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(1, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using rational numbers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(8, 9)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = Fraction(2, 3)\n",
    "y = Fraction(3, 4)\n",
    "# 2/3 / 3/4 --> 2/3 * 4/3 --> 8/9\n",
    "Fraction(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using floats:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(1, 8)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(0.125)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(1, 2)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using strings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(21, 2)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction('10.5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(22, 7)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction('22/7')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fractions are automatically reduced:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(1, 2)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(8, 16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Negative sign is attached to the numerator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(-1, 4)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(1, -4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Standard arithmetic operators are supported:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(1, 1)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(1, 3) + Fraction(1, 3) + Fraction(1, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(1, 8)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(1, 2) * Fraction(1, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(3, 2)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(1, 2) / Fraction(1, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can recover the numerator and denominator (integers):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "x = Fraction(22, 7)\n",
    "print(x.numerator)\n",
    "print(x.denominator)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since floats have **finite** precision, any float can be converted to a rational number:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "884279719003555/281474976710656\n",
      "3.141592653589793\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "x = Fraction(math.pi)\n",
    "print(x)\n",
    "print(float(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6369051672525773/4503599627370496\n"
     ]
    }
   ],
   "source": [
    "x = Fraction(math.sqrt(2))\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that these rational values are approximations to the irrational numbers $\\pi$ and $\\sqrt{2}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Beware!!**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Float number representations (as we will examine in future lessons) do not always have an exact representation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The number 0.125 (1/8) **has** an exact representation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(1, 8)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(0.125)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and so we see the expected equivalent fraction.\n",
    "\n",
    "But, 0.3 (3/10) does **not** have an exact representation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(3, 10)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(3, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "but"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(5404319552844595, 18014398509481984)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(0.3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will study this in upcoming lessons.\n",
    "\n",
    "But for now, let's just see a quick explanation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = 0.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.3\n"
     ]
    }
   ],
   "source": [
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Everything looks ok here - why am I saying 0.3 (float) is just an approximation?\n",
    "\n",
    "Python is trying to format the displayed value for readability - so it rounds the number for a better display format!\n",
    "\n",
    "We can instead choose to display the value using a certain number of digits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.30000'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "format(x, '.5f')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At 5 digits after the decimal, we might still think 0.3 is an exact representation.\n",
    "\n",
    "But let's display a few more digits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.300000000000000'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "format(x, '.15f')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hmm... 15 digits and still looking good!\n",
    "\n",
    "How about 25 digits..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.2999999999999999888977698'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "format(x, '.25f')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we see that **x** is not quite 0.3..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In fact, we can quantify the delta this way:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "delta = Fraction(0.3) - Fraction(3, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Theoretically, delta should be 0, but it's not:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(-1, 90071992547409920)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**delta** is a very small number, the above fraction...\n",
    "\n",
    "As a float:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.1102230246251566e-17"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float(delta)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Constraining the denominator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "884279719003555/281474976710656\n",
      "3.1415926535897931159979635\n"
     ]
    }
   ],
   "source": [
    "x = Fraction(math.pi)\n",
    "print(x)\n",
    "print(format(float(x), '.25f'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22/7\n",
      "3.1428571428571427937015414\n"
     ]
    }
   ],
   "source": [
    "y = x.limit_denominator(10)\n",
    "print(y)\n",
    "print(format(float(y), '.25f'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "311/99\n",
      "3.1414141414141414365701621\n"
     ]
    }
   ],
   "source": [
    "y = x.limit_denominator(100)\n",
    "print(y)\n",
    "print(format(float(y), '.25f'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "355/113\n",
      "3.1415929203539825209645642\n"
     ]
    }
   ],
   "source": [
    "y = x.limit_denominator(500)\n",
    "print(y)\n",
    "print(format(float(y), '.25f'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
